﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Account_Change_Log
{
    public partial class Form : FormPage
    {
        BLL.Account_Change_Log bll = new BLL.Account_Change_Log();
        Model.Account_Change_Log model = new Model.Account_Change_Log();
        //
        int StoreID = 0, SupplierID = 0,UserID=0;
        BLL.Store bllStore = new BLL.Store();
        Model.Store modelStore = new Model.Store();
        BLL.Supplier bllSupplier = new BLL.Supplier();
        Model.Supplier modelSupplier = new Model.Supplier();
        protected void Page_Load(object sender, EventArgs e)
        {
            StoreID = DataTypePlus.requestInt(Request.QueryString["StoreID"]);
            SupplierID = DataTypePlus.requestInt(Request.QueryString["SupplierID"]);
            if (StoreID > 0)
            {
                modelStore = bllStore.GetModel(StoreID);
                UserID = modelStore.UserID;
            }
            else if (SupplierID > 0)
            {
                modelSupplier = bllSupplier.GetModel(SupplierID);
                UserID = modelSupplier.UserID;
            }
            else { Master.ScriptManager_js(Master.ScriptManager_Alert_float_Str("参数错误", false) + Master.ScriptManager_close_adminRight_Str()); }
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            rbl_Reason.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.AccountChangeReason.Recharge), ((int)EnumHelper.AccountChangeReason.Recharge).ToString()));
            rbl_Reason.Items.Add(new ListItem(EnumHelper.GetEnumDescription(EnumHelper.AccountChangeReason.Draw), ((int)EnumHelper.AccountChangeReason.Draw).ToString()));
            rbl_Reason.SelectedIndex = 0;
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            decimal Money = DataTypePlus.requestDecimal(tb_Money.Text);
            EnumHelper.AccountChangeReason ChangeReason = (EnumHelper.AccountChangeReason)DataTypePlus.requestInt(rbl_Reason.SelectedValue);
            EnumHelper.UserType UserType = EnumHelper.UserType.Supplier;
            int SupplierOrStoreID = SupplierID;
            if (StoreID > 0)
            {
                UserType = EnumHelper.UserType.Store;
                SupplierOrStoreID = StoreID;
            }

            

            if (Master.noErr( Money == 0,"金额必须填写")) 
            {
                CallBak<int> callBak = BLL.Account_Change_Log.Add(UserType, SupplierOrStoreID, ChangeReason, SupplierOrStoreID, Money);
                if (!callBak.IsSuccess) Master.ScriptManager_Alert_float(callBak.ErrMsg, false);
                else
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Account_Change_Log, callBak.ReturnVal, EnumHelper.OperateType.Add, EnumHelper.GetEnumDescription(ChangeReason) + StringPlus.showMoney(Money));
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), StoreID > 0 ? EnumHelper.TableName.Store : EnumHelper.TableName.Supplier, SupplierOrStoreID, EnumHelper.OperateType.Update, EnumHelper.GetEnumDescription(ChangeReason) + StringPlus.showMoney(Money));

                    //通知
                    BLL.Message bllMessage = new BLL.Message();
                    bllMessage.NewMessage(0, Security.GetCurrentUserID(), UserID,
                        "您的汇款共计" + StringPlus.showCoin(Money) + "元已收到并打入您的帐号，请查阅",
                        true, true);
                    //model.StoreID = StoreID;
                    //model.SupplierID = SupplierID;
                    //model.Account = Money;
                    //model.Type = (int)ChangeType;
                    //model.Reason = (int)EnumHelper.AccountChangeReason.Recharge;
                    //if (StoreID > 0) model.AccountBefore = modelStore.Account;
                    //else if (SupplierID > 0) model.AccountBefore = modelSupplier.Account;
                    //if (ChangeType == EnumHelper.AccountChangeType.Increase) model.AccountAfter = model.AccountBefore + model.Account;
                    //else model.AccountAfter = model.AccountBefore - model.Account;
                    //int LogID_new = bll.Add(model);
                    //BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Money_Change_Log, LogID_new, EnumHelper.OperateType.Add, "");
                    ////
                    //if (StoreID > 0)
                    //{
                    //    modelStore.Account = model.AccountAfter;
                    //    bllStore.Update(modelStore);
                    //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, StoreID, EnumHelper.OperateType.Update, EnumHelper.GetEnumDescription(ChangeType) + StringPlus.showMoney(Money));
                    //}
                    //else if (SupplierID > 0)
                    //{
                    //    modelSupplier.Account = model.AccountAfter;
                    //    bllSupplier.Update(modelSupplier);
                    //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, SupplierID, EnumHelper.OperateType.Update, EnumHelper.GetEnumDescription(ChangeType) + StringPlus.showMoney(Money));
                    //}
                    Master.SubmitOK();
                }
            }
        }
    }
}